package com.szsh.aiot.hsm.utils;

import java.nio.charset.Charset;
/**
 * @Description:
 * @Date 2023/10/30: 15:41
 * @Author: LOADING>>>>>
 */




public final class Hex {
    private static final int UNSIGNED_BYTE_MASK = 255;
    private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    private static final String[] BYTE2HEX_PAD = new String[256];
    private static final String[] BYTE2HEX_NOPAD = new String[256];
    private static final String[] HEXPADDING = new String[16];
    private static final String[] BYTEPADDING = new String[16];
    private static final String[] BYTE2HEX = new String[256];
    private static final char[] BYTE2CHAR = new char[256];
    private static final int HEX = 16;
    private static final int MASK_TAIL_4BITS = 15;
    private static final int MASK_PRE_4BITS = 240;
    private static final String[] HEXCODE;
    private static final String NEWLINE = "\n";

    public static String byte2HexString(byte bSrc) {
        int high = (bSrc & 240) >> 4;
        int low = bSrc & 15;
        return HEXCODE[high] + HEXCODE[low];
    }

    public static String byte2HexString(byte[] bArray) {
        StringBuilder hexBuf = new StringBuilder();

        for(int i = 0; i < bArray.length; ++i) {
            hexBuf.append(byte2HexString(bArray[i]));
        }

        return hexBuf.toString();
    }

    public static String byte2HexString(byte[] buf, int offset, int len) {
        byte[] tmp = new byte[len];
        System.arraycopy(buf, offset, tmp, 0, len);
        return byte2HexString(tmp);
    }

    public static String formatBytes(byte[] src) {
        int length = src.length;
        int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
        StringBuilder dump = new StringBuilder(rows * 80);
        dump.append("         +-------------------------------------------------+\n         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |\n+--------+-------------------------------------------------+----------------+");

        int index;
        int remainder;
        int j;
        for(index = 0; index < length; ++index) {
            remainder = index & 15;
            if (remainder == 0) {
                dump.append("\n");
                dump.append(Long.toHexString((long)index & 4294967295L | 4294967296L));
                dump.setCharAt(dump.length() - 9, '|');
                dump.append('|');
            }

            dump.append(BYTE2HEX[src[index] & 255]);
            if (remainder == 15) {
                dump.append(" |");

                for(j = index - 15; j <= index; ++j) {
                    dump.append(BYTE2CHAR[src[j] & 255]);
                }

                dump.append('|');
            }
        }

        if ((index & 15) != 0) {
            remainder = length & 15;
            dump.append(HEXPADDING[remainder]);
            dump.append(" |");

            for(j = index - remainder; j < index; ++j) {
                dump.append(BYTE2CHAR[src[j] & 255]);
            }

            dump.append(BYTEPADDING[remainder]);
            dump.append('|');
        }

        dump.append("\n+--------+-------------------------------------------------+----------------+");
        return dump.toString();
    }

    public static String formatHexString(String hexStr) {
        return formatBytes(sHex2byte(hexStr));
    }

    public static byte[] sHex2byte(String sHex) {
        int srcLen = sHex.length() / 2;
        byte[] bHex = new byte[srcLen];
        int loc = 0;
        String temp = null;

        for(int i = 0; i < srcLen; ++i) {
            temp = String.valueOf(sHex.charAt(i * 2)) + sHex.charAt(i * 2 + 1);
            bHex[loc++] = (byte)Integer.parseInt(temp, 16);
        }

        return bHex;
    }

    public static void sHex2byte(String sHex, byte[] buf, int offset, int len) {
        byte[] bHex = sHex2byte(sHex);
        System.arraycopy(bHex, 0, buf, offset, len);
    }

    public static String toHexString(String srcStr) {
        return byte2HexString(srcStr.getBytes(DEFAULT_CHARSET));
    }

    public static String toHexString(String srcStr, Charset charset) {
        return byte2HexString(srcStr.getBytes(charset));
    }

    private Hex() {
    }

    static {
        int i;
        int padding;
        StringBuilder buf;
        int j;
        for(i = 0; i < HEXPADDING.length; ++i) {
            padding = HEXPADDING.length - i;
            buf = new StringBuilder(padding * 3);

            for(j = 0; j < padding; ++j) {
                buf.append("   ");
            }

            HEXPADDING[i] = buf.toString();
        }

        for(i = 0; i < BYTEPADDING.length; ++i) {
            padding = BYTEPADDING.length - i;
            buf = new StringBuilder(padding);

            for(j = 0; j < padding; ++j) {
                buf.append(' ');
            }

            BYTEPADDING[i] = buf.toString();
        }

        for(i = 0; i < 10; ++i) {
            buf = new StringBuilder(2);
            buf.append('0');
            buf.append(i);
            BYTE2HEX_PAD[i] = buf.toString();
            BYTE2HEX_NOPAD[i] = String.valueOf(i);
        }

        while(i < 16) {
            buf = new StringBuilder(2);
            char c = (char)(97 + i - 10);
            buf.append('0');
            buf.append(c);
            BYTE2HEX_PAD[i] = buf.toString();
            BYTE2HEX_NOPAD[i] = String.valueOf(c);
            ++i;
        }

        while(i < BYTE2HEX_PAD.length) {
            buf = new StringBuilder(2);
            buf.append(Integer.toHexString(i));
            String str = buf.toString();
            BYTE2HEX_PAD[i] = str;
            BYTE2HEX_NOPAD[i] = str;
            ++i;
        }

        for(i = 0; i < BYTE2HEX.length; ++i) {
            BYTE2HEX[i] = ' ' + BYTE2HEX_PAD[i & 255];
        }

        for(i = 0; i < BYTE2CHAR.length; ++i) {
            if (i > 31 && i < 127) {
                BYTE2CHAR[i] = (char)i;
            } else {
                BYTE2CHAR[i] = '.';
            }
        }

        HEXCODE = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
    }
}

